<template>
  <!-- 维修套餐维护 -->
  <div>
    <!--条件筛选-->
    <TopFilter :list="topFilterList" :cols="3" labelWidth="90" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <!--操作 -->
    <button-area :style="{ paddingLeft: '80px' }"></button-area>
    <!-- 列筛选-->
    <FilterTable
      ref="table"
      columnsRef="repair-package-index"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectData"

    >
      <!--列操作-->
      <template slot="controls">
          <multiuse-button size="small" icon="el-icon-upload" @click="showImportFun()">导入</multiuse-button>
        <el-button size="small" type="primary" icon="el-icon-upload" @click="downHandle">模板下载</el-button>
      </template>
    </FilterTable>

     <!--抽屉 -->
    <BaseDialog
      :visible.sync="showImport"
      :closable="false"
      icon="el-icon-upload"
      title="导入"
      width="800px"
      destroyOnClose
      :containerStyle="{ height: 'calc(100% - 120px)', overflow: 'auto', paddingBottom: '60px' }"
    >
      <importExcel @close="showImport = false" @importclose="close()" :url="`${config.uploadUrl}/api/dmscloud.activity/activity/importActivityCountsItem`" />
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils';
import importExcel from './importExcel';
import SERVER from '@/api/server';
import config from '@/assets/js/config';
import { queryActivityCountAll, importActivityCountsItem, getAllUser,deleteActivityCount,downActivityCount} from '@/api/repair/repairBusiness/repairPackage';

// 延保订单查询
export default {
  name: 'repair-package',
  components: { importExcel},
  mixins: [dictionary],
  data() {
    return {
      server: SERVER,
      form: {
        fileList: []
      },
      showAddPackage: false,
      showAddPackageTitle: '新增套餐',
      fetchParams: { xhrAbort: false },
      dialogTitle: '',
      fetchApi: queryActivityCountAll,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      tableData: [],
      selectFlag: false,
      topFilterList: this.createTopFilterList(),
      estimate: false,
      environmentalDate: {},
      serviceAdvisorList: [],
      wxlxList: [],
      onChangeColumn: [],
      activityCode: '',
      selectedRows:[],
      showImport: false,
      config: config,
    };
  },
  created() {
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.getAllUser();
  },
  methods: {

    async getAllUser() {
      let res = await getAllUser();
      if (res.resultCode === 200) {
        let allUser = res.data.map(x => ({
          text: x.employeeName,
          value: x.userId + ''
        }));
        this.columns.find(x => x.dataIndex === 'releaseBy').dictItems = allUser;

      }
    },
   /*下载模板*/
    downHandle() {
      this.handleType = 'down';
      this.$confirm('是否将套餐次数维护导入模板保存至本地文件夹?', '提示', {
        confirmButtonText: '下载',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          downActivityCount()
            .then((res) => {
              console.log(res, 'res');
              exportExcelFile(res.data, '套餐次数维护导入模板');
            })
            .catch((err) => {});
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消',
          });
        });
    },

  // Excel更新导入
    showImportFun() {
      this.showImport = true;
    },
    close() {
      this.showImport = false;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },

    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin'
        },
        {
          type: 'INPUT',
          label: '套餐代码',
          fieldName: 'activityCode'
        },
        // {
        //   type: 'SELECT',
        //   label: '状态',
        //   fieldName: 'releaseTag',
        //   itemList: this.createDictList(1014, [10141001,10141002,10141004,10141005,10141006])
        // },

      ];
    },
    createTableColumns() {
       return [
         {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          // fixed: 'left',
          width: 70,
          render: props => {
            return (
              <div>
                <el-button size='mini' disabled={props.row.usedCounts>=1} type='text' onClick={() => this.invalid(props.row)}>
                  删除
                </el-button>
              </div>
            );
          }
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          width: 180
        },
        {
          title: '套餐代码',
          dataIndex: 'activityCode',
          width: 200,
        },
        {
          title: '套餐名称',
          dataIndex: 'activityName',
          width: 130
        },
        {
          title: '套餐次数',
          dataIndex: 'counts',
          width: 130
        },
        {
          title: '状态',
          dataIndex: 'releaseTag',
           editType: 'select',
          // width: 120,
          editItems: this.createDictList('1001'),
        },
        {
          title: '套餐使用次数',
          dataIndex: 'usedCounts',
          width: 130
        },
        {
          title: '套餐剩余次数',
          dataIndex: 'surplusCounts',
          width: 130
        },

        {
          title: '创建人',
          dataIndex: 'releaseBy'
        },
        {
          title: '创建时间',
          dataIndex: 'releaseTime',
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          width: 140,
        },
      ];
    },

    async invalid(row) {
        this.$confirm('是否确认删除，删除后该套餐则无效', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        console.log('确定',row,row.itemId);
        const res = await deleteActivityCount({itemId:row.itemId});
        if (res.resultCode === 200) {
          this.$notify.success({ title: '提示信息', message: '删除成功' });
          this.fetchParams = { ...this.fetchParams };
        }
      console.log('确定1');
      }).catch(() => {
        console.log('取消');
      });

    },
    //选中
    selectData(rows) {
      this.selectedRows = rows;
      this.disableds = this.selectedRows.length <= 0;
      console.log(this.selectedRows);
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },

    closeEstimate(val, type) {
      // 关闭
      this.estimate = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
  }
};
</script>

<style lang="less" scoped></style>
